package com.example.myapplication;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

/**
 * 儿童保护Activity
 * 包含禁用时间段入口和密码设置
 */
public class ParentalControlActivity extends AppCompatActivity {

    private ParentalControlRepository repository;
    private BlockTimeRepository blockTimeRepository;
    private TopicRepository topicRepository;

    // UI组件
    private LinearLayout layoutBlockTime;
    private LinearLayout layoutBlockedTopic;
    private LinearLayout layoutSetPassword;
    private LinearLayout layoutClosePassword;
    private TextView textBlockTimeSummary;
    private TextView textTopicSummary;
    private TextView textPasswordStatus;
    private View dividerPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parental_control_new);

        repository = ParentalControlRepository.getInstance(this);
        blockTimeRepository = new BlockTimeRepository(this);
        topicRepository = new TopicRepository(this);
        
        initViews();
        setupListeners();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateBlockTimeSummary();
        updateTopicSummary();
        updatePasswordStatus();
    }

    private void initViews() {
        layoutBlockTime = findViewById(R.id.layout_block_time);
        layoutBlockedTopic = findViewById(R.id.layout_blocked_topic);
        layoutSetPassword = findViewById(R.id.layout_set_password);
        layoutClosePassword = findViewById(R.id.layout_close_password);
        textBlockTimeSummary = findViewById(R.id.text_block_time_summary);
        textTopicSummary = findViewById(R.id.text_topic_summary);
        textPasswordStatus = findViewById(R.id.text_password_status);
        dividerPassword = findViewById(R.id.divider_password);
    }

    private void setupListeners() {
        layoutBlockTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到禁用时间段列表页面
                Intent intent = new Intent(ParentalControlActivity.this, BlockTimeListActivity.class);
                startActivity(intent);
            }
        });

        layoutSetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到设置密码页面
                Intent intent = new Intent(ParentalControlActivity.this, SetPasswordActivity.class);
                startActivity(intent);
            }
        });

        layoutClosePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClosePasswordDialog();
            }
        });

        layoutBlockedTopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到话题管理页面
                Intent intent = new Intent(ParentalControlActivity.this, BlockedTopicListActivity.class);
                startActivity(intent);
            }
        });
    }

    private void updateBlockTimeSummary() {
        List<BlockTimeSlot> blockTimes = blockTimeRepository.getAllBlockTimes();
        
        if (blockTimes.isEmpty()) {
            textBlockTimeSummary.setText("暂未设置");
        } else {
            // 统计启用的时间段数量
            int enabledCount = 0;
            for (BlockTimeSlot slot : blockTimes) {
                if (slot.isEnabled()) {
                    enabledCount++;
                }
            }
            
            if (enabledCount == 0) {
                textBlockTimeSummary.setText("已设置 " + blockTimes.size() + " 个时段（未启用）");
            } else {
                textBlockTimeSummary.setText("已启用 " + enabledCount + " 个时段");
            }
        }
    }

    private void updateTopicSummary() {
        List<BlockedTopic> topics = topicRepository.getAllTopics();
        
        if (topics.isEmpty()) {
            textTopicSummary.setText("暂未设置");
        } else {
            // 统计启用的话题数量
            int enabledCount = 0;
            for (BlockedTopic topic : topics) {
                if (topic.isEnabled()) {
                    enabledCount++;
                }
            }
            
            if (enabledCount == 0) {
                textTopicSummary.setText("已设置 " + topics.size() + " 个话题（未启用）");
            } else {
                textTopicSummary.setText("已启用 " + enabledCount + " 个话题");
            }
        }
    }

    private void updatePasswordStatus() {
        String password = repository.getPassword();
        boolean hasPassword = password != null && !password.isEmpty();
        
        if (hasPassword) {
            textPasswordStatus.setText("更改密码");
            layoutClosePassword.setVisibility(View.VISIBLE);
            dividerPassword.setVisibility(View.VISIBLE);
        } else {
            textPasswordStatus.setText("设置密码");
            layoutClosePassword.setVisibility(View.GONE);
            dividerPassword.setVisibility(View.GONE);
        }
    }

    private void showClosePasswordDialog() {
        new android.app.AlertDialog.Builder(this)
            .setTitle("确认关闭安全密码吗？")
            .setPositiveButton("确认关闭", new android.content.DialogInterface.OnClickListener() {
                @Override
                public void onClick(android.content.DialogInterface dialog, int which) {
                    repository.setPassword("");
                    updatePasswordStatus();
                    Toast.makeText(ParentalControlActivity.this, "密码已关闭", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("取消", null)
            .show();
    }

}
